package com.example.controller;

import com.example.exception.CacheOperationException;
import com.example.model.Promotion;
import com.example.service.PromotionCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/promotion-cache")
public class PromotionCacheController {
    
    @Autowired
    private PromotionCacheService promotionCacheService;

    @PostMapping("/batch-build")
    public ResponseEntity<String> batchBuildCache(@RequestBody List<Promotion> promotions) {
        try {
            promotionCacheService.batchBuildCache(promotions);
            return ResponseEntity.ok("批量缓存构建成功");
        } catch (CacheOperationException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("批量缓存构建失败: " + e.getMessage());
        }
    }

    @GetMapping("/batch-query")
    public ResponseEntity<Map<String, List<String>>> batchQueryPromotions(
            @RequestParam List<String> skus,
            @RequestParam List<String> skuLabels) {
        try {
            Map<String, List<String>> result = 
                promotionCacheService.batchQueryPromotions(skus, skuLabels);
            return ResponseEntity.ok(result);
        } catch (CacheOperationException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Collections.emptyMap());
        }
    }
}